package com.wuwei.elearning;

import com.wuwei.elearning.utils.TrayService;
import lombok.SneakyThrows;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import java.net.URL;

@SpringBootApplication
@EnableTransactionManagement
@MapperScan({"com.wuwei.elearning.workbench.mapper","com.wuwei.elearning.question.mapper","com.wuwei.elearning.setting.mapper"})
public class ELearningApplication {


    public static void main(String[] args) {

        // 因为springboot默认是不支持图形界面的，设置系统属性，启用图形界面
        System.setProperty("java.awt.headless", "false");
//
//        ELearningApplication eLearningApplication = new ELearningApplication();
//
//        eLearningApplication.initSystemTray();
//
//        SpringApplication.run(ELearningApplication.class, args);

        ApplicationContext context = SpringApplication.run(ELearningApplication.class, args);

        // 获取 TrayService 并初始化系统托盘
        TrayService trayService = context.getBean(TrayService.class);
        trayService.initSystemTray();

    }

//    private TrayIcon trayIcon = new  TrayIcon(new ImageIcon(this.getClass().getClassLoader().getResource("learn.png"), "photo").getImage());



//    /**
//     * 初始化系统托盘
//     */
//    @SneakyThrows
//    private  void initSystemTray() {
//        if (SystemTray.isSupported()) {
////            URL resource = this.getClass().getClassLoader().getResource("learn.png");
////            ImageIcon imageIcon = new ImageIcon(resource,"photo");
////            Image image = imageIcon.getImage();
////            TrayIcon trayIcon = new TrayIcon(image);
//            trayIcon.setImageAutoSize(true);
//            trayIcon.setToolTip("e_learning系统");
//            //设置托盘菜单
//            PopupMenu pm = new PopupMenu() ;
//            MenuItem mi = new MenuItem("Exit") ;
//            MenuItem mi2 = new MenuItem("Enter");
//
//            ActionListener listener = new ActionListener() {
//                @SneakyThrows
//                @Override
//                public void actionPerformed(ActionEvent e) {
//                    // 处理菜单点击事件
//                    if (e.getActionCommand().equals("Exit")) {
//                        // 点击了Exit菜单
//
//                        System.exit(0);
//                    } else if (e.getActionCommand().equals("Enter")) {
//                        // 点击了Enter菜单
//                        // 执行相应的操作
//
//                        URI uri = new URI("http://localhost");
//
//                        Desktop desktop = Desktop.getDesktop();
//
//                        desktop.browse(uri);
//
//                    }
//                }
//            };
//
//            pm.add(mi2);
//
//            pm.add(mi);
//
//            mi.addActionListener(listener);
//            mi2.addActionListener(listener);
//            trayIcon.setPopupMenu(pm);
//            trayIcon.addMouseListener(new MouseAdapter(){
//                @SneakyThrows
//                @Override
//                public void mouseClicked(MouseEvent e) {
//                    if (e.getClickCount() == 2) {
//                        URI uri = new URI("http://localhost");
//
//                        Desktop desktop = Desktop.getDesktop();
//
//                        desktop.browse(uri);
//                    }
//                }
//            });
//
//            SystemTray.getSystemTray().add(trayIcon);
//        } else {
//            System.out.println("系统不支持托盘");
//        }
//    }

}
